import { _decorator, Component, Node, Size, screen, view } from "cc"
import { AudioManager } from "../framework/AudioManager"
import { Tween } from "cc"
import { gtPlayable } from "../GtPlayable"
import ResManager from "../framework/ResManager"
import { Prefab } from "cc"
import { instantiate } from "cc"
import { Label } from "cc"
import { GlobalData } from "../GlobalData"
import { UIEndingHpItem } from "./UIEndingHpItem"
const { ccclass, property } = _decorator

@ccclass("EndingView")
export class EndingView extends Component {
  @property(Node)
  nodeMask: Node = null!

  @property(Node)
  nodeOther: Node = null!

  @property(Node)
  nodeBtn1: Node = null!

  @property(Node)
  nodeBtn2: Node = null!

  @property(Node)
  nodeParent: Node = null!

  @property(Label)
  labCoinValue: Label = null!

  @property(Label)
  labExpValue: Label = null!

  @property(Label)
  labGemValue: Label = null!

  @property(Node)
  nodeMVP: Node = null!

  @property(Prefab)
  preUIItem: Prefab = null!

  private arrPosX: number[] = []

  start() {
    AudioManager.instance.playEffect("sm_fail")

    this.nodeMask.alpha = 0
    new Tween(this.nodeMask).to(0.3, { alpha: 1 }).start()
    let scale = this.nodeOther.scaleX
    this.nodeOther.alpha = 0
    new Tween(this.nodeOther).to(0.3, { scaleXYZ: scale }, { easing: "backOut" }).start()
    new Tween(this.nodeOther).to(0.3, { alpha: 1 }).start()

    for (let i: number = 0; i < 5; i++) {
      ResManager.instance.loadRes("prefabs/game/role/role" + (i + 1), Prefab, (err, prefab: Prefab) => {
        if (prefab) {
          const node = instantiate(prefab)
          this.nodeParent.addChild(node)
          node.setPosition(this.arrPosX[i], 54)
          const nodeHpItem = instantiate(this.preUIItem)
          nodeHpItem.setPosition(0, -147)
          node.addChild(nodeHpItem)
          const uiItem = nodeHpItem.getComponent(UIEndingHpItem)
          uiItem.init(i)
        }
      })
    }

    const time: number = 0.3
    this.scheduleOnce(() => {
      // new Tween(this.nodeBtn1)
      //   .to(time, { scaleXYZ: 1.1 })
      //   .to(time * 2, { scaleXYZ: 0.9 })
      //   .to(time, { scaleXYZ: 1 })
      //   .delay(4 * time)
      //   .union()
      //   .repeatForever()
      //   .start()

      // new Tween(this.nodeBtn2)
      //   .delay(4 * time)
      //   .to(time, { scaleXYZ: 1.1 })
      //   .to(time * 2, { scaleXYZ: 0.9 })
      //   .to(time, { scaleXYZ: 1 })
      //   .union()
      //   .repeatForever()
      //   .start()
      new Tween(this.nodeBtn1)
      .delay(time)
      .to(time, { scaleXYZ: 1.1 })
      .to(time * 2, { scaleXYZ: 0.9 })
      .to(time * 2, { scaleXYZ: 1.1 })
      .to(time, { scaleXYZ: 1 })
      .delay(7 * time)
      .union()
      .repeatForever()
      .start()

    new Tween(this.nodeBtn2)
      .delay(8 * time)
      .to(time, { scaleXYZ: 1.1 })
      .to(time * 2, { scaleXYZ: 0.9 })
      .to(time * 2, { scaleXYZ: 1.1 })
      .to(time, { scaleXYZ: 1 })
      .union()
      .repeatForever()
      .start()
    }, 0.6)

    this.labCoinValue.string = GlobalData.instance.coinValue + ""
    this.labExpValue.string = GlobalData.instance.expValue + ""
    this.labGemValue.string = GlobalData.instance.gemValue + ""
  }

  private updateAdapter() {
    let winSize = new Size(screen.windowSize.width / view["_scaleX"], screen.windowSize.height / view["_scaleY"])
    if (winSize.width > winSize.height) {
      this.nodeOther.scaleXYZ = (winSize.height * 1920) / 1080 / winSize.width
    } else {
      this.nodeOther.scaleXYZ = 1
    }

    const distance: number = winSize.width > winSize.height ? 300 : 150
    for (let i: number = 0; i < this.arrPosX.length; i++) {
      this.arrPosX[i] = distance * i - distance * 2
      const node = this.nodeParent.children[i]
      if (node) {
        node.positionX = this.arrPosX[i]
      }
    }
    this.nodeMVP.positionX = this.arrPosX[0] + 80
  }

  protected onEnable(): void {
    for (let i: number = 0; i < 5; i++) {
      this.arrPosX.push(-600 + i * 300)
    }

    this.updateAdapter()
    view.on("canvas-resize", this.updateAdapter, this)
  }

  protected onDisable(): void {
    view.off("canvas-resize", this.updateAdapter, this)
    this.unscheduleAllCallbacks()
  }

  onBtnDownload() {
    gtPlayable.install()
  }

  update(deltaTime: number) {}
}
